package com.lwq.controller.system;

import com.lwq.annotation.PermissionController;
import com.lwq.annotation.PermissionInterface;
import com.lwq.annotation.RepeatSubmit;
import com.lwq.controller.BaseController;
import com.lwq.model.Dto;
import com.lwq.model.Dtos;
import com.lwq.model.Result;
import com.lwq.model.system.SysRole;
import com.lwq.service.system.RoleService;
import com.lwq.util.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2021/6/10
 */
@PermissionController(type = "system")
@RestController
public class RoleController extends BaseController {

    @Autowired
    private RoleService service;

    @GetMapping(value = "/role/list")
    @PermissionInterface(name = "查询角色", code = "role.query")
    public TableDataInfo list() {
        Dto dto = Dtos.newDto(request);
        startPage();
        return getDataTable(service.queryList(dto));
    }

    @GetMapping(value = "/role/user")
    @PermissionInterface(name = "查询角色", code = "role.query")
    public Result roleUser(@RequestParam Long userId) {
        return Result.ok(service.queryRoleUser(userId));
    }

    @GetMapping(value = "/role/menu")
    @PermissionInterface(name = "查询角色", code = "role.query")
    public Result roleMenu(@RequestParam Long roleId) {
        return Result.ok(service.queryRoleMenu(roleId));
    }

    @GetMapping(value = "/role/permission")
    @PermissionInterface(name = "查询角色", code = "role.query")
    public Result rolePermission(@RequestParam Long roleId) {
        return Result.ok(service.queryRolePermission(roleId));
    }

    @PostMapping(value = "/role")
    @RepeatSubmit
    @PermissionInterface(name = "新增角色", code = "role.add")
    public Result add(@RequestBody @Valid SysRole role) {
        service.add(role);
        return Result.ok();
    }

    @PutMapping(value = "/role")
    @RepeatSubmit
    @PermissionInterface(name = "修改角色", code = "role.update")
    public Result update(@RequestBody @Valid SysRole role) {
        service.update(role);
        return Result.ok();
    }

    @DeleteMapping(value = "/role/{id}")
    @PermissionInterface(name = "删除角色", code = "role.remove")
    public Result remove(@PathVariable Long id) {
        service.del(id);
        return Result.ok();
    }

    @PutMapping(value = "/role/permission")
    @RepeatSubmit
    @PermissionInterface(name = "关联角色", code = "role.rel")
    public Result relPermission(@RequestParam Long roleId, @RequestParam List<Long> permissionIds) {
        service.relRolePermissions(roleId, permissionIds);
        return Result.ok();
    }

    @PutMapping(value = "/role/menu")
    @RepeatSubmit
    @PermissionInterface(name = "关联角色", code = "role.rel")
    public Result relMenu(@RequestParam Long roleId, @RequestParam List<Long> menusIds) {
        service.relRoleMenus(roleId, menusIds);
        return Result.ok();
    }

    @PutMapping(value = "/role/user")
    @RepeatSubmit
    @PermissionInterface(name = "关联角色", code = "role.rel")
    public Result relUser(@RequestParam Long userId, @RequestParam(required = false) List<Long> roleIds) {
        service.relUserRole(userId, roleIds);
        return Result.ok();
    }

}
